﻿using SmallJob.Model;
using SmallShop.Services.Interfaces;
using System.Web.Mvc;
using System.Web.Security;
using Caching;

namespace BackEnd.Controllers
{
    public class HomeController : Controller
    {
        private readonly IManagerService _ManagerService;

        public HomeController(IManagerService ManagerService)
        {
            _ManagerService = ManagerService;
        }

        //
        // GET: /Home/
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Example()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string email, string password)
        {
            var manager = _ManagerService.GetByEmail(email);
            if (manager != null)
            {
                if (password.Equals(manager.Password))
                {
                    FormsAuthentication.SetAuthCookie(manager.ProperName, false);

                    return RedirectToAction("Index", "Home");
                }
            }
            return View();
        }
         [Authorize]
        public ActionResult Logout()
        {
            if (User.Identity.IsAuthenticated)
            {
                FormsAuthentication.SignOut();
            }
            return RedirectToAction("Login");
        }
        public ActionResult ClearCache() {
            var r = MemCache.ClearCache();
            return Json(r);
        }
    }
}